<?php

class ClassYun extends ClassOption {

    private $oConfig;

    public function __construct($hvConfig) {
        parent::__construct($hvConfig);
        $this->oConfig = $hvConfig['oConfig'];
    }

    public function FormatMoney($money) {
        return number_format($money, 2, '.', '');
    }

    public function OrderTransAdd($oDb, $order_id, $trade_amount, $trade_type, $trade_title) {
        $iOperAmount = floatval($trade_amount);
        if ($iOperAmount === 0) {
            $this->error("操作金额不能为0 In OrderTransAdd");
        }
        $oDb->begin();
        $mWhere = array();
        $mWhere['id'] = $order_id;
        $oSql = $oDb->sql()->table('order')->where($mWhere);
        $sField = 'id,deleted,balance';
        $mRowOrder = $oSql->field($sField)->lock()->find();
        if (empty($mRowOrder)) {
            $this->error("order:id={$order_id}不存在 In OrderTransAdd");
        }
        if ($mRowOrder['deleted']) {
            $this->error("mRowOrder->id={$mRowOrder['id']}已标记删除状态");
        }
        //将用户余额原本的单位就是元
        $balance_before = floatval($mRowOrder['balance']);
        $balance_after = $balance_before + $iOperAmount;
        if ($balance_after < 0) {
            //余额不能小于0
            $sMsg = "订单的剩余可用余额不足";
            $sMsg .= "\r\n(order_id={$order_id})";
            $sMsg .= "\r\n(balance_before={$balance_before})";
            $sMsg .= "\r\n(iOperAmount={$iOperAmount})";
            $sMsg .= "\r\n(balance_after={$balance_after})";
            $sMsg .= "\r\n(OrderTransAdd)";
            $this->error($sMsg, -101, 'nobal');
        }
        $aSave = array();
        $aSave['balance'] = $balance_after;
        $oSql->save($aSave);
        $aAdd = array();
        $aAdd['order_id'] = $mRowOrder['id'];
        $aAdd['action'] = $trade_type;
        $aAdd['title'] = $trade_title;
        $aAdd['amount'] = $iOperAmount; //消费金额应填入负数
        $aAdd['balance_before'] = $balance_before;
        $aAdd['balance_after'] = $balance_after;
        $sTable = 'order_trans';
        return $oDb->sql()->table($sTable)->add($aAdd);
    }

    public function NtuserBalanceLogAdd($db_yun, $create_uid, $ntuser_user_id, $acctype, $amount, $title) {
        $iOperAmount = floatval($amount);
        if ($iOperAmount === 0) {
            $this->error("操作金额不能为0 In NtuserBalanceLogAdd");
        }
        $db_yun->begin();
        // 1:首先取得$mRowNtuserBalance
        $sField = 'id,username,balance';
        $oSql = $db_yun->sql()->table('ntuser_balance')->where(array('user_id' => $ntuser_user_id, 'acctype' => $acctype));
        $mRowNtuserBalance = $oSql->field($sField)->lock()->find();
        if (empty($mRowNtuserBalance)) {
            $this->error("ntuser_user_id={$ntuser_user_id}[{$acctype}]账户不存在 In NtuserBalanceLogAdd");
        }
        // 2:将新的余额写入
        $balance_before = floatval($mRowNtuserBalance['balance']);
        $balance_after = $balance_before + $iOperAmount;
        if ($balance_after < 0) {
            //余额不能小于0
            $sMsg = "余额不足";
            $sMsg .= "\r\n(ntuser_user_id={$ntuser_user_id})";
            $sMsg .= "\r\n(balance_before={$balance_before})";
            $sMsg .= "\r\n(iOperAmount={$iOperAmount})";
            $sMsg .= "\r\n(balance_after={$balance_after})";
            $sMsg .= "\r\n(NtuserBalanceLogAdd)";
            $this->error($sMsg, -101, 'nobal');
        }
        $aSave = array();
        $aSave['balance'] = $balance_after;
        $oSql->save($aSave);
        $mAdd = array();
        $mAdd['create_uid'] = $create_uid;
        $mAdd['balance_id'] = $mRowNtuserBalance['id'];
        $mAdd['user_id'] = $ntuser_user_id;
        $mAdd['username'] = $mRowNtuserBalance['username'];
        $mAdd['title'] = $title;
        $mAdd['balance_acctype'] = $acctype;
        $mAdd['balance_before'] = $balance_before;
        $mAdd['amount'] = $iOperAmount; //消费金额应填入负数
        $mAdd['balance_after'] = $balance_after;
        return $db_yun->sql()->table('ntuser_balance_log')->add($mAdd);
    }

}
